package com.springboot.quanxian.controller.sys;
import cn.dev33.satoken.stp.StpUtil;
import com.springboot.quanxian.common.ResultEnum;
import com.springboot.quanxian.entity.Notice;
import com.springboot.quanxian.entity.SysManager;
import com.springboot.quanxian.entity.req.SysManagerReq;
import com.springboot.quanxian.entity.req.SysRoleReq;
import com.springboot.quanxian.entity.search.SysManagerSearch;
import com.springboot.quanxian.entity.view.SysManagerView;
import com.springboot.quanxian.exception.MyException;
import com.springboot.quanxian.qpage.QPage;
import com.springboot.quanxian.service.ISysManagerService;
import com.springboot.quanxian.util.ResultView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping("/api/admin/manager")
public class SysManagerController {

    @Autowired
    private ISysManagerService iSysManagerService;

    /**
     * 账号列表
     * @return
     */
    @GetMapping("/getPageList")
    public ResultView list(QPage qPage, SysManagerSearch obj) {
        return this.iSysManagerService.getListAll(qPage,obj);
    }

    /**
     * 用户不分页列表
     * @return
     */
    @GetMapping("/getSelectList")
    public ResultView getSelectList(SysManagerSearch obj) {
        return this.iSysManagerService.getSelectList(obj);
    }


    /**
     * 用户详情
     * @param managerId
     * @return
     */
    @GetMapping("/getManagerId")
    public ResultView getManagerId(@NotBlank(message = "账户ID不能为空") String managerId) {
        return this.iSysManagerService.getManagerId(managerId);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @PostMapping("/add")
    public ResultView add(SysManager obj) {
        return this.iSysManagerService.add(obj);
    }


    /**
     * 修改员工赋予角色
     * @param obj
     * @return
     */
    @PostMapping("/up")
    public ResultView up(SysManager obj) {
        return this.iSysManagerService.up(obj);
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping(value = "/getManagerInfo")
    public ResultView getManagerInfo() {
        String managerId=StpUtil.getLoginIdAsString();
        SysManager sysManager=this.iSysManagerService.getById(managerId);
        SysManagerView sysManagerView=new SysManagerView();
        BeanUtils.copyProperties(sysManager,sysManagerView);
        return ResultView.ok(sysManagerView);
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/del")
    public ResultView del(@NotBlank(message = "ID不能为空") String id) {
        boolean isOk= this.iSysManagerService.removeById(id);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"删除失败，删除数据异常");
        }
        return ResultView.ok();
    }

    /**
     * 修改密码
     * @param pwd
     * @param pwd
     * @return
     */
    @PostMapping("/updatePwd")
    public ResultView updatePwd(@NotBlank(message = "旧密码不能为空") String pwd,
                                @NotBlank(message = "新密码不能为空") String newPwd,
                                @NotBlank(message = "确认密码不能为空") String againPwd) {
        String managerId=StpUtil.getLoginIdAsString();
        return this.iSysManagerService.updatePwd(managerId,pwd,newPwd,againPwd);
    }




}
